package com.fyy.socket.socketClient;

import com.fyy.socket.model.Student;

import java.io.*;
import java.net.Socket;

/**
 * @Author fyy
 * @Description Socket客户端
 * @Date 2020/11/30 15:08
 */
public class SocketClient {

    private static Socket s;
    private static String ip = "127.0.0.1";
    private static int port = 8399;
    private static InputStream input;//字节流输出
    private static OutputStream output;//字节流输入
    private static ObjectInputStream objin = null;
    private static ObjectOutputStream obj = null;

    static {
        try {
            s = new Socket (ip, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readContent(){
        try {
            input = s.getInputStream ();
            objin = new ObjectInputStream (input); //读取字节流
            int a = objin.readInt (); //获取整数
            double b = objin.readDouble (); //获取浮点数
            Student stu = (Student) objin.readObject ();//readObject要转换你设定的对象
            return a + ":" + b + ":" + stu;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void sendContent(int id,String name){
        try {
            output = s.getOutputStream ();
            obj = new ObjectOutputStream(output);
            Student ss = new Student (id, name);
            obj.writeObject (ss);
            obj.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
